package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCommentMapper;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/18 15:07
 */
@Service
public class DtsCommentServiceImpl implements DtsCommentService {

    @Autowired
    DtsCommentMapper commentMapper;
    @Override
    public Map<String, Object> selectByPage(Integer page, Integer limit, String sort, String order, Integer usersId, Integer goodId, List<Integer> brandsIds) {
        IPage<DtsComment> iPage = new Page<>(page,limit);

        QueryWrapper<DtsComment> wrapper = new QueryWrapper<>();
        if ("desc".equalsIgnoreCase(order)) {
            wrapper.orderByDesc("c."+sort);
        }else {
            wrapper.orderByAsc("c."+sort);
        }


        if (usersId != null){
            wrapper.eq("c.user_id",usersId);
        }
        if (goodId != null){
            wrapper.eq("c.value_id",goodId);
        }
        if (brandsIds != null && brandsIds.size() > 0){
            wrapper.in("g.brand_id", brandsIds);
        }

        commentMapper.selectCommentAndBrandPage(iPage,wrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        map.put("items", iPage.getRecords());

        return map;
    }

    @Override
    public void delete(DtsComment comment) {
        commentMapper.deleteById(comment);
    }
}
